using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class StaticCarGroupIndicator : MonoBehaviour
{
	[SerializeField]
	private float distanceForShow = 3f;

	[SerializeField]
	private Vector3 offset = new Vector3(0f, 2f, 0f);

	[SerializeField]
	private List<Transform> staticObj = new List<Transform>();

	[SerializeField]
	private GameObject _indicatorPrefab;

	private static GameObject _indicator;

	private IEnumerator chkDistance;

	private GameObject indicator
	{
		get
		{
			if (_indicator == null)
			{
				_indicator = new GameObject();
				Object.Instantiate(_indicatorPrefab).transform.SetParent(_indicator.transform);
				_indicator.name = "StaticCarIndicator";
			}
			return _indicator;
		}
	}

	private void Start()
	{
		if (staticObj.Count == 0)
		{
			Transform[] componentsInChildren = GetComponentsInChildren<Transform>(true);
			foreach (Transform transform in componentsInChildren)
			{
				if (transform.parent == base.transform)
				{
					staticObj.Add(transform);
				}
			}
		}
		indicator.SetActive(false);
	}

	private void OnTriggerEnter(Collider other)
	{
		if (!(other.transform != GameController.instance.playerScript.transform))
		{
			chkDistance = ChkDistance(other.transform);
			StartCoroutine(chkDistance);
		}
	}

	private void OnTriggerExit(Collider other)
	{
		if (!(other.transform != GameController.instance.playerScript.transform))
		{
			StopCoroutine(chkDistance);
			indicator.SetActive(false);
		}
	}

	private IEnumerator ChkDistance(Transform other)
	{
		Transform minDistTransform2 = null;
		while (true)
		{
			minDistTransform2 = null;
			for (int i = 0; i < staticObj.Count; i++)
			{
				float lastDist = Vector3.Distance(other.position, staticObj[i].position);
				if (distanceForShow > lastDist && (minDistTransform2 == null || lastDist < Vector3.Distance(minDistTransform2.position, other.position)))
				{
					minDistTransform2 = staticObj[i];
				}
			}
			if ((bool)minDistTransform2)
			{
				if (!indicator.GetActive())
				{
					indicator.SetActive(true);
				}
				indicator.transform.position = minDistTransform2.position + offset;
			}
			else if (indicator.GetActive())
			{
				indicator.SetActive(false);
			}
			yield return new WaitForSeconds(0.2f);
		}
	}
}
